package io.gitee.twoke.generator.engine.impl.template.impl;

import io.gitee.twoke.generator.engine.TemplateService;
import io.gitee.twoke.generator.engine.impl.ServiceImpl;
import io.gitee.twoke.generator.engine.impl.cfg.GenerateEngineConfigurationImpl;
import io.gitee.twoke.generator.engine.impl.cmd.*;
import io.gitee.twoke.generator.engine.impl.template.*;

public class TemplateServiceImpl extends ServiceImpl
implements TemplateService {

    public TemplateServiceImpl(GenerateEngineConfigurationImpl processEngineConfiguration) {
        super(processEngineConfiguration);
    }

    public TemplateServiceImpl() {
    }

    @Override
    public CreateTemplateBuilder createTemplate() {
        return commandExecutor.execute(ctx -> new CreateTemplateBuilderImpl(this));
    }

    @Override
    public CreateTemplateGroupBuilder createTemplateGroup() {
        return commandExecutor.execute(ctx -> new CreateTemplateGroupBuilderImpl(this));
    }

    @Override
    public Template findById(String id) {
        return commandExecutor.execute(ctx -> ctx.getTemplateEntityManager().findById(id));
    }

    public Template create(CreateTemplateBuilderImpl templateBuilder) {
        return commandExecutor.execute(new CreateTemplateCmd(templateBuilder));
    }

    public TemplateGroup create(CreateTemplateGroupBuilderImpl createTemplateGroupBuilder) {
        return commandExecutor.execute(new CreateTemplateGroupCmd(createTemplateGroupBuilder));
    }

    @Override
    public TemplateFileQuery createTemplateFileQuery() {
        return new TemplateFileQueryImpl(commandExecutor, generateEngineConfiguration.getDatabaseType());
    }

    @Override
    public void deleteTemplate(String id) {
        commandExecutor.execute(new DeleteTemplateCmd(id));
    }

    @Override
    public void deleteTemplateGroup(String id) {
        commandExecutor.execute(new DeleteTemplateGroupCmd(id));
    }

    @Override
    public void deleteTemplateFile(String id) {
        commandExecutor.execute(new DeleteTemplateFileCmd(id));
    }

    @Override
    public void bind(String groupId, String templateId) {
        commandExecutor.execute(new BindTemplateAndTemplateGroup(groupId, templateId));
    }

    @Override
    public void unbind(String groupId, String templateId) {
        commandExecutor.execute(new UnBindTemplateAndTemplateGroup(groupId, templateId));
    }
}
